package by.jtraining.london.web;

import java.util.Set;

import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;

public final class DestroyGuiceFinalizerOnShutdownListener implements ServletContextListener {

  @Override
  public void contextInitialized(ServletContextEvent sce) {
    /* Nothing special here. */
  }

  @SuppressWarnings("deprecation")
  @Override
  public void contextDestroyed(ServletContextEvent sce) {
    Set<Thread> allThreads = Thread.getAllStackTraces().keySet();
    for (Thread thread : allThreads) {
      if ("com.google.inject.internal.util.$Finalizer".equals(thread.getName())) {
        thread.stop();
      }
    }
  }

}
